﻿package igz.fleaxy.ui.preloader {
	
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * Clase que crea un preloader con efecto glow simple
	 * 
	 * @param	p_settings : Object = { "frontLayer": displayObject
	 * 								  , "textEnable": Boolean
	 * 								  , "textSize": uint
	 * 								  , "textColor": uint				
	 * 								  , "textDisplace": Point	
	 * 							      , "glowColor": uint
	 *							      , "glowAlpha": Number
	 *							      , "strengthGlow": uint
	 * 								  }
	 * 
	 * @example GlowPreloaderUi( { "frontLayer":  displayObject
	 * 						     , "textEnable": false
	 * 							 , "textSize": 15
	 * 							 , "textColor": 0xFFFFFF
	 * 							 , "textDisplace": new Point(5, -25);
	 *							 , "glowColor": 0xFFFFFF
	 *							 , "glowAlpha": 1
	 *							 , "strengthGlow" 2
	 * 							 } );
	 */
	
	public class GlowPreloaderUi extends Sprite {
	
		private const _K : Number = 1.0;
		
		private var _FrontLayer : Sprite;
		private var _CounterText : TextField; 
		
		private var _Settings : Object = { "frontLayer": null
										,  "textEnable": false
										,  "textSize":  15
										,  "textColor": 0xFFFFFF
										,  "textDisplace": new Point()
										,  "glowColor": 0xFFFFFF
										,  "glowAlpha": 1
										,  "strengthGlow": 1
										};
		
		public function GlowPreloaderUi( p_settings:Object = null ) {
			
			// Combinamos opciones
			ObjectUtil.$Merge( p_settings, _Settings );
			
			// Capa frontal
			_FrontLayer = new Sprite();
			addChild( _FrontLayer );
			
			if ( _Settings["frontLayer"] == null ) {
				_Load_DefaultPreloader();
			}
			else {
				_Load_CustomPreloader();
			}
		}
		
		// Método que establece un cargador por defecto
		private function _Load_DefaultPreloader() : void {
			
			// Capa frontal
			_FrontLayer.graphics.lineStyle( 1, 0xFFFFFF, 1, true );
			_FrontLayer.graphics.beginFill( 0x084B8A, 1 );
			_FrontLayer.graphics.drawRect( 0, 0, 100, 100 );
			_FrontLayer.graphics.endFill();
					
			// Contador de progresion
			_Create_CounterText();
		}
		
		
		// Método que establece un cargador personalizado
		private function _Load_CustomPreloader() : void {
									
			// Capa frontal
			_FrontLayer.addChild( _Settings["frontLayer"] );
			
			// Contador de completado
			if ( _Settings["textEnable"] ) {
				_Create_CounterText();
			}
		}
		
		// Método que establece un contador prar el cargador
		private function _Create_CounterText() : void {
			_CounterText = new TextField();
			_CounterText.defaultTextFormat = new TextFormat( "Verdana", _Settings["textSize"],_Settings["textColor"] );
			//_CounterText.textColor = _Settings["textColor"];
			_CounterText.selectable = _CounterText.multiline = false;
			addChild( _CounterText );
			
			_CounterText.y = ( _FrontLayer.height * 0.5) - _Settings["textSize"] + _Settings["textDisplace"].y;
		}
		
		// Establece un valor
		public function $SetValue( p_value:uint ) : void {
			_UpdatePreloader( p_value );
		}
		
		private function _UpdatePreloader( p_value:uint ) : void {
			_FrontLayer.filters = [ new GlowFilter(  _Settings["glowColor"]
												   , _Settings["glowAlpha"]
												   , p_value / _K
												   , p_value / _K
												   , _Settings["strengthGlow"] ) ];
			
			if ( _CounterText ) {
				_Update_CounterText( p_value );
			}
		}
		
		protected function _Update_CounterText( p_value:uint ) : void {
			_CounterText.htmlText = String( p_value + " % ");
			_CounterText.x = ( _FrontLayer.width  - _CounterText.textWidth ) * 0.5 + _Settings["textDisplace"].x;
		}
	}

}